//
//  BKMain_RecommendCell.m
//  bestkeep
//
//  Created by utouu666 on 17/2/14.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKMain_RecommendCell.h"
#import "BKMainCommon.h"

@interface BKMain_RecommendCell()

@property(nonatomic,strong)UIImageView *goodsImageView;
@property(nonatomic,strong)UILabel *bigTitleLabel;
@property(nonatomic,strong)UILabel *smallTitleLabel;
@property(nonatomic,strong)UILabel *goodsPriceLabel;
@property(nonatomic,strong)BKMain_StrikeLabel *marketPriceLabel;
@property (nonatomic, strong) UILabel *marketLabel;
@property(nonatomic,strong)bkjxModel *model;

@end
@implementation BKMain_RecommendCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushToGoodsDetail:)];
        [self addGestureRecognizer:gesture];

        [self initialize];
    }
    return self;
}

-(void)initialize{
    _goodsImageView = [UIImageView new];
    _goodsImageView.userInteractionEnabled = YES;
    [self addSubview:_goodsImageView];
    [_goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(22);
        make.top.mas_equalTo(self.mas_top).offset(12);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-12);
        make.width.mas_equalTo(self.goodsImageView.mas_height);
    }];
    
    _bigTitleLabel = [UILabel new];
    _bigTitleLabel.font = [Common systemFontOfSize:14];
    _bigTitleLabel.numberOfLines = 1;
    _bigTitleLabel.textColor = RGBACOLOR(64, 64, 64, 1);
    [self addSubview:_bigTitleLabel];
    [_bigTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.goodsImageView.mas_right).offset(28);
        make.top.mas_equalTo(self.mas_top).offset(17);
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.height.mas_offset(@(21));
    }];
    
    _smallTitleLabel = [UILabel new];
    _smallTitleLabel.font = [Common systemFontOfSize:12];
    _smallTitleLabel.textColor = RGBACOLOR(102, 102, 102, 1);
    [self addSubview:_smallTitleLabel];
    [_smallTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.goodsImageView.mas_right).offset(28);
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.top.mas_equalTo(self.bigTitleLabel.mas_bottom).offset(5*ScreenWidth/375);
        make.height.mas_offset(@(17));
    }];
    
    
    _marketLabel = [UILabel new];
    _marketLabel.text = @"市场价：";
    _marketLabel.textColor = RGB(102, 102, 102);
    _marketLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:_marketLabel];
    [_marketLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(-15*ScreenWidth/375);
        make.left.equalTo(self.goodsImageView.mas_right).offset(28);
        make.width.equalTo(@50);
    }];
    
    _marketPriceLabel = [BKMain_StrikeLabel new];
    _marketPriceLabel.font = [Common systemFontOfSize:13];
    _marketPriceLabel.textColor = RGBACOLOR(136, 136, 136, 1);
    _marketPriceLabel.strikeThroughColor = RGBACOLOR(136, 136, 136, 1);
    _marketPriceLabel.strikeThroughEnabled = YES;
    _marketPriceLabel.strkeLineStyle = StrikeLineStyleMiddle;
    [self addSubview:_marketPriceLabel];
    [_marketPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(-15*ScreenWidth/375);
        make.left.equalTo(self.marketLabel.mas_right);
        make.right.equalTo(self.mas_right);
    }];
    _goodsPriceLabel = [UILabel new];
    _goodsPriceLabel.font = [Common systemFontOfSize:16];
    _goodsPriceLabel.textColor = RGBACOLOR(255, 16, 16, 1);
    [self addSubview:_goodsPriceLabel];
    [_goodsPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_goodsImageView.mas_right).offset(28);
        make.right.mas_equalTo(self.mas_right);
        make.bottom.equalTo(self.marketLabel.mas_top).offset(-6*ScreenWidth/375);
    }];
    _lineView = [UIView new];
    _lineView.backgroundColor = RGB(242, 242, 242);
    [self addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom);
        make.left.equalTo(self.goodsImageView.mas_right).offset(27);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(@1);
    }];
 
    
    UIButton *buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [buyBtn setBackgroundColor:RGB(3, 181, 152)];
    [buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
    [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    buyBtn.titleLabel.font = [Common systemFontOfSize:12];
    if (iPhone4s || iPhone5) {
        buyBtn.titleLabel.font = [Common systemFontOfSize:11];
    }
    buyBtn.layer.masksToBounds = YES;
    buyBtn.layer.cornerRadius = 2.5;
    [buyBtn addTarget:self action:@selector(pushToGoodsDetail:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:buyBtn];
    [buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.marketPriceLabel.mas_bottom);
        make.right.mas_equalTo(self.mas_right).offset(-10);
        make.width.mas_offset(@65);
        make.height.mas_offset(@25);
        if (iPhone5 || iPhone4s) {
            make.width.mas_offset(@50);
            make.height.mas_offset(@22);
        }
    }];
}
-(void)pushToGoodsDetail:(UITapGestureRecognizer *)tap{
    
    Mediator *mediator = [Mediator sharedInstance];
    UIViewController *detailVC = [mediator BKGD_goodsDetailViewController:_model.goodsId saleTypes:_model.saleTypes];
    detailVC.view.backgroundColor = [UIColor whiteColor];
    [[self viewController].navigationController pushViewController:detailVC animated:YES];
}
//得到视图控制器
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

- (void)fillData:(id)data{
    if (!data) {
        return;
    }
    _model = [[bkjxModel alloc]init];
    [_model setValuesForKeysWithDictionary:data];
    [_goodsImageView load:_model.goodsCoverImg placeholderImage:[UIImage imageNamed:@"BKFeatured_newGoods"]];
    _bigTitleLabel.text = _model.goodsSpecialDescribe;
    _smallTitleLabel.text = _model.goodsName;
    NSString *str = [NSString stringWithFormat:@"%@",[data objectForKey:@"goodsMarketPrice"]];
    _marketPriceLabel.text = str;
    if (_model.upText == nil) {
        _model.upText = @"";
    }
    
    _goodsPriceLabel.attributedText = [BKMainCommon specialStringStyle:[NSString stringWithFormat:@"%@%@", _model.goodsPrice, _model.upText] fontOfSize:14];
    
    
}

+(CGSize)calculateSize:(id)data{
    if (!data) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(SCREENWIDTH, (ScreenWidth < 375) ? (123*ScreenWidth/375.f + 20) : (123*ScreenWidth/375.f));
}
@end
